package com.lagou.edu.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author lyj
 * @Title: AutoDeliverController
 * @ProjectName lagou-parent
 * @Description: TODO
 * @date 2020/12/27 11:21
 */
@RestController
@RequestMapping("/autodeliver")
public class AutoDeliverController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
//    @GetMapping("/checkState/{userId}")
//    public Integer findResumeOpenState(@PathVariable Long userId) {
////        Integer forObject =restTemplate.getForObject("http://localhost:8080/resume/openstate/" + userId,
////                        Integer.class);
////        System.out.println("======>>>调⽤简历微服务，获取到⽤户" + userId + "的默认简历当前状态为： " + forObject);
//
//
//        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("lagou-service-resume");
//        ServiceInstance serviceInstance = serviceInstanceList.get(0);
//        String host = serviceInstance.getHost();
//        int port = serviceInstance.getPort();
//        String url="http://"+host+":"+port+"/resume/openstate/"+userId;
//
//        Integer forObject =restTemplate.getForObject(url,Integer.class);
//        System.out.println("======>>>从eureka server获取服务提供者实例："+url);
//        return forObject;
//    }
//@GetMapping("/checkState/{userId}")
//public Integer findResumeOpenState(@PathVariable Long userId) {
//    String url="http://lagou-service-resume/resume/openstate/"+userId;
//    Integer forObject =restTemplate.getForObject(url,Integer.class);
//    System.out.println("======>>>从eureka server获取服务提供者实例："+url);
//    return forObject;
//}

    @GetMapping("/checkStateTimeout/{userId}")
    @HystrixCommand(
            //线程池 的标识，要保持唯一，不然与其它方法相同名的线程池就共用
            threadPoolKey = "checkStateTimeout",
            //线程数细节配置
            threadPoolProperties = {
                    @HystrixProperty(name="coreSize",value = "1"),  //线程数（同时运行的线程数）
                    @HystrixProperty(name="maxQueueSize",value = "20") //等待队列长度
            },
            commandProperties = {
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",
                    value = "2000")
            }
    )
    public Integer checkStateTimeout(@PathVariable Long userId) {
        String url="http://lagou-service-resume/resume/openstate/"+userId;
        Integer forObject =restTemplate.getForObject(url,Integer.class);
        System.out.println("======>>>从eureka server获取服务提供者实例："+url);
        return forObject;
    }


    @GetMapping("/checkStateTimeoutFallBack/{userId}")
    @HystrixCommand(
            //线程池 的标识，要保持唯一，不然与其它方法相同名的线程池就共用
            threadPoolKey = "findResumeOpenStateFallBack",
            //线程数细节配置
            threadPoolProperties = {
                    @HystrixProperty(name="coreSize",value = "2"),  //线程数（同时运行的线程数）
                    @HystrixProperty(name="maxQueueSize",value = "20") //等待队列长度
            },

            commandProperties = {
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",
                            value = "2000"),
                    //8秒钟内，请求次数达到2个，并且失败率在50%以上，就跳闸
                    //跳闸后活动窗⼝设置为3s
                    @HystrixProperty(name =
                            "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    @HystrixProperty(name =
                            "circuitBreaker.requestVolumeThreshold",value = "2"),
                    @HystrixProperty(name =
                            "circuitBreaker.errorThresholdPercentage",value = "50"),
                    @HystrixProperty(name =
                            "circuitBreaker.sleepWindowInMilliseconds",value = "3000")

            },
            fallbackMethod = "myFallback"
    )
    public Integer findResumeOpenStateFallBack(@PathVariable Long userId) {
        String url="http://lagou-service-resume/resume/openstate/"+userId;
         Integer forObject =restTemplate.getForObject(url,Integer.class);
        System.out.println("======>>>从eureka server获取服务提供者实例："+url);
        return forObject;
    }

    //预设方法
    public Integer myFallback(Long userId){
        return -1; //返回预设值（兜底数据）
    }
}

